<?php

defined('BASEPATH') OR exit('No direct script access allowed');
//---------------------------------------------------------
//|	消息模块
//---------------------------------------------------------
//| @author rable
//---------------------------------------------------------
class Message extends MY_Controller {
	
	/**
	 * 消息模型
	 * @var ConsumerMessageModel
	 */
	public $mMsg;
	
	public function __construct(){
		parent::__construct();
	}
	
	
	/**
	 * 获取基本信息
	 */
	public function getBase(){
		
		
		$tA = array(
				'order_total'=>'52',
				'system_total'=>'85',
		);
		
		$this->appReturn(1,'',$tA);
	}
	
	/**
	 * 获取消息列表
	 */
	public function getMsgList(){
		//表单验证
		$vConf = array(
				array('type', '类型', 'required|in_list[1,3]'),
				array('page', '分页', 'required|is_natural_no_zero'),
		);
		$this->formDataValidation($vConf);
		
		$type = post('type');
		$page = post('page');
		
		$this->loadModel('ConsumerMessageModel','mMsg','consumer');
		
		$tA = $this->mMsg->appGetList($this->userId, $type,$page);
		$this->appReturn(1,'',$tA);
	}

	/**
	 * 获取消息详情
	 */
	public function getInfo(){
		//表单验证
		$vConf = array(
				array('message_id', '消息id', 'required|is_natural_no_zero'),
		);
		$this->formDataValidation($vConf);
		
		$messageId = post('message_id');
		
		$this->loadModel('ConsumerMessageModel','mMsg','consumer');

		$tA = $this->mMsg->getInfo($messageId,$this->userId);
		if (! $tA){
			$this->appReturn(0,'数据为空');
		}


        //保存查看记录
        if($tA['viewed'] == 1){
            $tMsg = array(
                'message_id' => $messageId,
                'consumer_id' => $this->userId,
                'order_id' => $tA['order_id'],
                'is_delete' => 1,
                'addtime' => time(),
            );
            if(!($this->mMsg->saveView($tMsg))){
                $this->appReturn(0, '保存失败');
            }
        }

        $html = $this->view('message/detail',['html'=>$tA],0,0,0,true);

        $this->ajaxReturn(1, '', $html);

	}
	
	/**
	 * 删除消息
	 */
	public function rmMsg(){
		//表单验证
		$vConf = array(
				array('message_id', '消息id', 'required|is_natural_no_zero'),
		);
		$this->formDataValidation($vConf);
		
		$messageId = post('message_id');
		
		$this->loadModel('ConsumerMessageModel','mMsg','consumer');
		
		$tA = $this->mMsg->getInfo($messageId,$this->userId);
		if (! $tA){
			$this->appReturn(0,'数据为空');
		}
        if($tA['viewed'] == 1){
            $this->appReturn(0, '未读消息不能删除');
        }
		if ($tA['type'] == 2){
			//个人信息，真删除
			$this->db->where(array('id'=>$messageId,'consumer_id'=>$this->userId));
			if ( ! $this->db->delete($this->mMsg->tbMessage)){
				$this->appReturn(0,'删除失败');
			}
			
			//删除浏览状态
			$this->db->where(array('message_id'=>$messageId,'consumer_id'=>$this->userId));
			if (! $this->db->delete($this->mMsg->tbMessageViewed)){
				$this->appReturn(0,'删除失败');
			}
		}else{
			
			//假删除
			if ( ! $this->mMsg->saveStatus(array('is_delete'=>2),$messageId,$this->userId)){
				$this->appReturn(0,'删除失败');
			}
		}
		
		$this->appReturn(1,'已删除');
	}
}